﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Extension;

public partial class TrainClassManagement_CourseCalendar :AdminUserBase
{
    private int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["ID"] != null)
        {
            Id = Convert.ToInt32(Request["ID"]);
        }
        if (!this.IsPostBack)
        {
            //bindCourseList("", " CourseID,VCL_startDate,VCL_startTime  ");
            //GetCourse();
            
            this.caleyear.SelectedValue = DateTime.Today.Year.ToString();
            this.calemon.SelectedValue = DateTime.Today.Month.ToString();
            
            CreateCourseCalendar(int.Parse(caleyear.SelectedValue), int.Parse(calemon.SelectedValue)); 
        }
             
    }
    private void CreateCourseCalendar(int currYear, int currMonth )
    {
        StringBuilder sbCalendarTemp = new StringBuilder();
        StringBuilder sbCourseTemp = new StringBuilder();//按课程分类的课表
        StringBuilder sbListTemp = new StringBuilder();//列表式课表
        int currDay = 1;
        CourseListModule service = new CourseListModule();
        DataSet ds = service.GetCourseByID(Id, CurrentUser.SchoolID, "", "");
        DayOfWeek beginWeekDay = new DateTime(currYear, currMonth, 1).DayOfWeek;
        DayOfWeek endWeekDay = new DateTime(currYear, currMonth, DateTime.DaysInMonth(currYear, currMonth)).DayOfWeek;
        bool isStarted = false;

        #region 生成日历的html,使用table
        sbCalendarTemp.AppendLine(" <table  class=\"tableCourse\" >");
        sbCalendarTemp.AppendLine(" <tr> <th style=\"color:Red\">星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th style=\"color:Green\">星期六</th> </tr>");
        for (int i = 1; i <= 6; i++)
        {
            sbCalendarTemp.AppendLine("<tr> ");
            for (int j = 0; j < 7; j++)
            {
                if (currDay <= DateTime.DaysInMonth(currYear, currMonth))
                {
                    if ((DayOfWeek)Enum.Parse(typeof(DayOfWeek), j.ToString()) == beginWeekDay)
                    {
                        //sbTemp.AppendLine(" <li><p><span class=\"day_in_moth\"></span><span class=\"day\">" + currDay + "</span></p>");
                        sbCalendarTemp.AppendLine("  <td>");
                        sbCalendarTemp.AppendLine(GetInnerTableHtml(currYear, currMonth, currDay,ds));
                        sbCalendarTemp.AppendLine("</td>");
                        isStarted = true;
                        currDay++;
                    }
                    else
                    {
                        if (isStarted == true)
                        {
                            sbCalendarTemp.AppendLine(" <td>");
                            sbCalendarTemp.AppendLine(GetInnerTableHtml(currYear, currMonth, currDay, ds));
                            sbCalendarTemp.AppendLine("</td>");
                            currDay++;
                        }
                        else
                        {
                            sbCalendarTemp.AppendLine(" <td >&nbsp;</td>");
                        }

                    }
                }
                else
                {
                    sbCalendarTemp.AppendLine(" <td >&nbsp;</td>");
                }
            }
            sbCalendarTemp.AppendLine("</tr>");

        }
        sbCalendarTemp.AppendLine("</table>");


        #endregion
        tableCourseList.InnerHtml = sbCalendarTemp.ToString();

       

        #region 按课程分类的课表
        //mid_r05.InnerHtml=CreateCourseByWeekHtml();
        #endregion
    }
    /// <summary>
    /// 获取日历课程表中，包含课程信息的html
    /// </summary>
    /// <param name="currYear"></param>
    /// <param name="currMonth"></param>
    /// <param name="currDay"></param>
    /// <returns></returns>
    private string GetInnerTableHtml(int currYear, int currMonth, int currDay, DataSet ds)
    {
        StringBuilder sbTemp = new StringBuilder();
        DateTime dtDate = new DateTime(currYear, currMonth, currDay);
        DataRow[] drCourseInfo = null;
       
       
      
        //if(UserCourseList==null)
        //{
        //    Response.Redirect(ResolveClientUrl("~/UserLogin.aspx")+"?ReturnUrl="+Request.Url);

        //}
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {

            drCourseInfo =
                ds.Tables[0].Select("VCL_startDate='" + dtDate.ToShortDateString() + "'",
                                                "VCL_startTime ");
        }

        StringBuilder sbTdAM = new StringBuilder();//上午课程
        StringBuilder sbTdPM = new StringBuilder();//下午课程
        sbTemp.Append(currDay);
        if (drCourseInfo != null)
        {
            if (drCourseInfo.Length > 0)
            {
                string courseInfo = string.Empty;
                string playJs = string.Empty;

                for (int k = 0; k < drCourseInfo.Length; k++)
                {
                    courseInfo = "课程：" + drCourseInfo[k]["VC_name"].ToString() + "   时间：" +
                                 DateTime.Parse(drCourseInfo[k]["VCL_startTime"].ToString()).ToString("HH:mm") + "-" +
                                 DateTime.Parse(drCourseInfo[k]["VCL_EndTime"].ToString()).ToString("HH:mm");
                    courseInfo += "    教室：" + drCourseInfo[k]["VR_roomName"].ToString();
                    playJs = string.Format("VideoPlay('{0}','{1}','{2}','{3}','{4}')", drCourseInfo[k]["ID"],
                                           dtDate.ToShortDateString(), drCourseInfo[k]["VR_roomName"],
                                           drCourseInfo[k]["VCL_startTime"], drCourseInfo[k]["VCL_EndTime"]);

                    if (DateTime.Parse(drCourseInfo[k]["VCL_startTime"].ToString()).Hour < 12)
                    {
                        sbTdAM.AppendFormat("<a href='UpdateCourseList.aspx?ID={0}'   onmouseover=\"this.title ='" + courseInfo + "'\">" +
                                          StringUtils.getLimitLengthString(drCourseInfo[k]["VC_name"].ToString(), 8,
                                                                           "") + "</a><br/>", drCourseInfo[k]["ID"]);
                    }
                    else
                    {
                        sbTdPM.AppendFormat("<a href='UpdateCourseList.aspx?ID={0}'   onmouseover=\"this.title ='" + courseInfo + "'\">" +
                                          StringUtils.getLimitLengthString(drCourseInfo[k]["VC_name"].ToString(), 8,
                                                                           "") + "</a><br/>", drCourseInfo[k]["ID"]);
                    }
                }













                //for (int k = 0; k < drCourseInfo.Length; k++)
                //{
                //    courseInfo = "课程：" + drCourseInfo[k]["VC_name"].ToString() + "   时间：" +
                //                 DateTime.Parse(drCourseInfo[k]["VCL_startTime"].ToString()).ToString("HH:mm") + "-" +
                //                 DateTime.Parse(drCourseInfo[k]["VCL_EndTime"].ToString()).ToString("HH:mm");
                //    courseInfo += "    教室：" + drCourseInfo[k]["VR_roomName"].ToString();
                //    playJs = string.Format("VideoPlay('{0}','{1}','{2}','{3}','{4}')", drCourseInfo[k]["ID"],
                //                           dtDate.ToShortDateString(), drCourseInfo[k]["RoomID"],
                //                           drCourseInfo[k]["VCL_startTime"], drCourseInfo[k]["VCL_EndTime"]);

                //    if (DateTime.Parse(drCourseInfo[k]["VCL_startTime"].ToString()).Hour < 12)
                //    {
                //        sbTdAM.AppendLine("<a href='javascript:' onclick=\"" + playJs +
                //                          "\" onmouseover=\"this.title ='" + courseInfo + "'\">" +
                //                          StringUtils.getLimitLengthString(drCourseInfo[k]["VC_name"].ToString(), 8,
                //                                                           "") + "</a><br/>");
                //    }
                //    else
                //    {
                //        sbTdPM.AppendLine("<a href='javascript:' onclick=\"" + playJs +
                //                          "\" onmouseover=\"this.title ='" + courseInfo + "'\">" +
                //                          StringUtils.getLimitLengthString(drCourseInfo[k]["VC_name"].ToString(), 8,
                //                                                           "") + "</a><br/>");
                //    }
                //}





                sbTemp.AppendLine(
                    " <div class=\"courseNameTd\">");
                sbTemp.AppendLine("<li class='left'>" + sbTdAM + "</li> ");
                sbTemp.AppendLine("<li class='right'>" + sbTdPM + " </li></div>");
            }
        }
        return sbTemp.ToString();
    }
    protected void Calendar_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateCourseCalendar(int.Parse(caleyear.SelectedValue), int.Parse(calemon.SelectedValue));
         
    }

}
